<?php
namespace Umpmobile\Controller;
use Think\Controller;
class ExtendController extends Controller {
	public $mixednick;
	public $sid;
	public $cid;
	public function  __construct (){
		parent::__construct();
		$this->mixednick = $_COOKIE["tida_mixnick"];
		// $this->mixednick = "kekydream"; 
		$this->sid=I("sid");
		$this->cid=I("cid");
		$fnick = urldecode(I("friendnick"));
		
		Vendor("taobaosdk.top.TopClient");
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		
		if(!$this->mixednick){
			$url = "https://hongbao.ews.m.jaeapp.com/?sid=".$this->sid."&cid=".$this->cid."&friendnick=".urlencode($fnick);
			$qcode = S($url);
			if(!$qcode){
				Vendor("taobaosdk.top.request.MaQrcodeCommonCreateRequest");
				$c = new \TopClient;
				$c->appkey = C("SM_KEY");//用后台的key
				$c->secretKey = C("SM_SECRET"); 
				$req = new \MaQrcodeCommonCreateRequest;
				$req->setType("url");
				$req->setContent($url);
				$req->setName("帮我砍价");
				$req->setStyle("EF4F2B");
				$req->setSize("300");
				$req->setNeedEps("false");
				$req->setLogo("http://img01.taobaocdn.com/imgextra/T1Od8YFT8eXXXXXXXX");
				$SHOP = M("Shop");
				$sessionKey = $SHOP->where("`sid`=".$this->sid)->getField("sessionkey");
				$resp = $c->execute($req, $sessionKey);
				$qcode = (String)$resp->modules->qrcode_d_o->qrcode_url;
			}
			
			$this->assign("qcode",$qcode); //分享二维码
			$this->assign("share_link",$url); //当前分享链接
			$this->display("no-nick");
			die();
		}
		
		if(cookie("avatar".$this->mixednick)){
			$this->avatar = cookie("avatar".$this->mixednick);
		}else{
			Vendor("taobaosdk.top.request.UserAvatarGetRequest");
			$c = new \TopClient;
			$c->appkey = C("SM_KEY");//用后台的key
			$c->secretKey = C("SM_SECRET"); 
			$req = new \UserAvatarGetRequest;
			$req->setNick($this->mixednick);
			$resp = $c->execute($req);
			$this->avatar = $resp->avatar?(string)$resp->avatar:"//img.alicdn.com/imgextra/i2/27390555/TB2QJUNlXXXXXXsXpXXXXXXXXXX-27390555.jpg";
			cookie("avatar".$this->mixednick,$this->avatar);
		}
		
		if($fnick && $this->cid && $this->sid){
			// 扫码即得的折扣处理
			$PROM = D("Promosions");	
			$cur_prom = current($PROM->where("`sid`=".$this->sid." AND `act_cat_id`=".$this->cid." AND `is_invite`=1")->getField("`id`,`user_tag_id`,`activity_id`,`act_cat_id`,`decrease_amount`"));
			if($cur_prom){
				$FRI = D("Friends");
				$FRI->sid=$this->sid;
				$FRI->cid=$this->cid;
				$FRI->friend = $this->mixednick;
				$FRI->getFriend();
			
				$PROM->sid = $this->sid;
				$PROM->cur_prom = $cur_prom;
				$PROM->my_nick = $this->mixednick; //只要是被邀请的（fnick为真），则当前人享受折扣
				$PROM->friend_info = $FRI->friend_info; 
				$re = $PROM->addUserTag();
			}
		}
		// $this->sid="116841492";//测试用
		// $this->mixednick="kekydream";//测试用
		$this->assign("sid",$this->sid);
		$this->assign("avatar",$this->avatar);
	}
}